//
//  ChatLookAddressViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/25.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "ChatLookAddressViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h> //引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h> //引入地图功能所有的头文件

@interface ChatLookAddressViewController () <BMKMapViewDelegate>
@property (nonatomic, strong) BMKMapView *mapView;
@end

@implementation ChatLookAddressViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"查看位置";
    
    [self initUI];
}
- (void)initUI {
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-50*Scale)];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    _mapView.showMapScaleBar = NO;
    [_mapView setZoomLevel:17];
    
    
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = [self.latitude floatValue];
    coor.longitude = [self.longitude floatValue];
    annotation.coordinate = coor;
    annotation.title = self.desc;
    [self.mapView addAnnotation:annotation];
    
    [self.mapView setCenterCoordinate:coor animated:YES];
    
    self.mapView.rotateEnabled = NO;
//    self.mapView.gesturesEnabled = NO;
    
    UIButton *navigationBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(self.mapView)+3*Scale, WIDTH-80*Scale, 44*Scale) imageName:nil target:self action:@selector(navigationBtnTap) title:@"导航" titleColor:COLOR_OF_WHITE];
    navigationBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    navigationBtn.layer.cornerRadius = 3*Scale;
    navigationBtn.layer.masksToBounds = YES;
    [self.view addSubview:navigationBtn];
}
#pragma mark - 去地图展示路线
/** 去地图展示路线 */
- (void)navigationBtnTap {
    // 后台返回的目的地坐标是百度地图的
    // 百度地图与高德地图、苹果地图采用的坐标系不一样，故高德和苹果只能用地名不能用后台返回的坐标
    CGFloat latitude  = self.latitude.floatValue;  // 纬度
    CGFloat longitude = self.longitude.floatValue; // 经度
    NSString *address = self.desc; // 送达地址
    
    // 打开地图的优先级顺序：百度地图->高德地图->苹果地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) { // 百度地图
        // 起点为“我的位置”，终点为后台返回的坐标
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=riding&src=快健康快递", latitude, longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    } else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { // 高德地图
        // 起点为“我的位置”，终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0", @"我的位置", address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        if (@available(iOS 10.0, *)) {//iOS10以后,使用新API
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束");
            }];
        } else {//iOS10以前,使用旧API
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
    } else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com"]]) { // 苹果地图
        // 起点为“我的位置”，终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@", address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else {
        // 快递员没有安装上面三种地图APP，弹窗提示安装地图APP
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请安装地图App" message:@"安装百度地图App，规划您出行路线，方便您的出行。" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertVC animated:NO completion:nil];
    }
}

@end
